package org.redis.cluster.statistics;

import java.util.concurrent.atomic.AtomicInteger;

import org.redis.cluster.event.Event;
import org.redis.cluster.event.EventListener;
import org.redis.cluster.interceptor.Interceptor;
import org.redis.cluster.interceptor.Invocation;
import org.redis.cluster.redis.transport.RedisReply;
import org.redis.cluster.redis.transport.reply.RedisTimeoutReply;
import org.redis.cluster.scheduler.second.SecondScheduleEvent;

public class TimeoutStatistics implements Interceptor,EventListener<SecondScheduleEvent> {
	
	private final static AtomicInteger totalCounter=new AtomicInteger();
	private final static AtomicInteger currentCounter=new AtomicInteger();

	@Override
	public RedisReply intercept(Invocation invocation) throws Throwable {
		RedisReply reply=invocation.invoke();
		if(reply instanceof RedisTimeoutReply) {
			totalCounter.incrementAndGet();
			currentCounter.incrementAndGet();
		}
		
		return reply;
	}

	@Override
	public void onEvent(SecondScheduleEvent event) {
		currentCounter.set(0);
	}

	@Override
	public boolean isSupport(Event event) {
		return (event instanceof SecondScheduleEvent);
	}
	
	public static int getCurrentCount() {
		return currentCounter.intValue();
	}
	
	public static int getTotalCount() {
		return totalCounter.intValue();
	}

}